
#include "net.h"



typedef struct
{
    unsigned int nYear;
    unsigned int nMonth;
    unsigned int nDay;
    unsigned int nHour;
    unsigned int nMin;
    unsigned int nSec;
    unsigned int DayIndex; /* 0 = Sunday */
} _time_struct;


static _time_struct   g_cur_time  ;

//{"sysTime2":"2023-03-08 14:27:01","sysTime1":"20230308142701"}
static int sunning_time_parse(const * json_data)
{
	int i = 0;
	int sys1_len = 0;
	int sys2_len = 0;

	char temp_buff[200] = {0};
	
	unsigned long long real_time = 0; 
		
	struct json_object* root = NULL; 
	struct json_object* sys1 = NULL; 
	struct json_object* sys2 = NULL;

	if(!json_data){
		return 0;
	}

	if(strlen(json_data) > 200){
		return 0;
	}

	log_i("json_data=%s\n",json_data);

	

#if 0	
	root = json_tokener_parse(json_data);
	if(root == NULL){
		return 0;
	}
	
	sys1 = json_object_object_get(root,"sysTime1"); 
	if(sys1 == NULL){
		json_object_put(root);
		return 0;
	}
	sys1_len = json_object_array_length(sys1);
	if(sys1_len <= 0){
		json_object_put(root);
		return 0;
	}

	memcpy(temp_buff,json_object_get_string(sys1),strlen(json_object_get_string(sys1)));

#if 0
	log_info("temp_buff=%s.\n",temp_buff);

	for(i=0;i<14;i++){
		log_info("temp_buff[%d]=%d\n",i,temp_buff[i]-'0');
	}
#endif

	g_cur_time.nYear  = (temp_buff[0] -'0')*1000 + (temp_buff[1] -'0')*100 + (temp_buff[2]-'0')*10 + (temp_buff[3]-'0');	
	g_cur_time.nMonth = (temp_buff[4] -'0')*10   + (temp_buff[5] -'0');
	g_cur_time.nDay   = (temp_buff[6] -'0')*10   + (temp_buff[7] -'0');

	g_cur_time.nHour  = (temp_buff[8] -'0')*10   + (temp_buff[9] -'0');
	g_cur_time.nMin   = (temp_buff[10]-'0')*10   + (temp_buff[11]-'0');
	g_cur_time.nSec   = (temp_buff[12]-'0')*10   + (temp_buff[13]-'0');

	if((g_cur_time.nYear > 9999)||(g_cur_time.nMonth > 13)||(g_cur_time.nDay > 32))
	{
		json_object_put(root);
		return 0;
	}
	if((g_cur_time.nHour > 25)||(g_cur_time.nMin > 61)||(g_cur_time.nSec > 61))
	{
		json_object_put(root);
		return 0;
	}	

	g_cur_time.DayIndex   = applib_dt_dayindex(g_cur_time.nYear,g_cur_time.nMonth,g_cur_time.nDay);


#if 0
	log_info("nYear   =%d\n",g_cur_time.nYear);
	log_info("nMonth  =%d\n",g_cur_time.nMonth);
	log_info("nDay    =%d\n",g_cur_time.nDay);
	log_info("nHour   =%d\n",g_cur_time.nHour);
	log_info("nMin    =%d\n",g_cur_time.nMin);
	log_info("nSec    =%d\n",g_cur_time.nSec);
	log_info("DayIndex=%d\n",g_cur_time.DayIndex);
#endif

	sys2 = json_object_object_get(root,"sysTime2"); 
	if(sys2 == NULL){
		json_object_put(root);
		return 0;
	}
	sys2_len = json_object_array_length(sys2);
	if(sys2_len <= 0){
		json_object_put(root);
		return 0;	
	}

	json_object_put(root);
	root = NULL;
#endif

	return 0;

}



static size_t time_respons_callback(void *buffer, size_t size, size_t nmemb, void *usr_data)
{
	//log_info("size:nmemb=%d:%d\n",size,nmemb);
	
	if((size*nmemb) > 200){
		return 0;
	}

	if(buffer){
		sunning_time_parse(buffer);
	}
	

    return size*nmemb;
}


int net_sunning_servr_time(void)
{	
	static CURL *keycurl = NULL;	 
	CURLcode res;	  	
	long	 retcode  = 0;
	CURLcode code ;


	if(keycurl == NULL){
		keycurl = curl_easy_init(); 	
	}
	if(keycurl == NULL){
		log_e("curl easy init error\n");
		return 0;
	}

	curl_easy_setopt(keycurl, CURLOPT_CUSTOMREQUEST, "GET");
	curl_easy_setopt(keycurl, CURLOPT_URL, "http://quan.suning.com/getSysTime.do");
	curl_easy_setopt(keycurl, CURLOPT_WRITEFUNCTION,  time_respons_callback);			
	curl_easy_setopt(keycurl, CURLOPT_WRITEDATA, NULL);

	curl_easy_setopt(keycurl, CURLOPT_TIMEOUT, 15);
	curl_easy_setopt(keycurl, CURLOPT_MAXREDIRS, 1);
	curl_easy_setopt(keycurl, CURLOPT_CONNECTTIMEOUT, 25);

	curl_easy_setopt(keycurl, CURLOPT_SSL_VERIFYPEER, 0); 
	curl_easy_setopt(keycurl, CURLOPT_SSL_VERIFYHOST, 0);
	res = curl_easy_perform(keycurl);
	if(res != CURLE_OK)
	{
		log_e("Failed to get res = %x\n",res); 	
	}	
	else
	{
		code = curl_easy_getinfo(keycurl, CURLINFO_RESPONSE_CODE , &retcode);
		if((code == CURLE_OK) && retcode == 200)
		{
			res  = CURLE_OK;
		}
		else
		{
			res = -1;
			log_e("getinfo error\n");
		}
	}

	curl_easy_cleanup(keycurl); 
	keycurl = NULL;
	
	return 0;
}








